﻿using System;
using System.Collections.Generic;
using System.Globalization;
using Medianamik.Core;
using Medianamik.Core.DAL;
using Medianamik.Core.DAL.Repositories;
using Medianamik.Core.DAL.Services;
using Medianamik.UI.Web.Shop.Data.Dto;
using SubSonic;
using System.Linq;

namespace Medianamik.UI.Web.Shop.Data.Repositories
{
    public class BrandRepository<TCollection, TItem> : MedianamikRepository<TCollection, TItem>, IBrandRepository<TItem>
        where TItem : Interfaces.IBrand, IRecordBase, new() 
        where TCollection : ICollectionWithChildren<TCollection, TItem>, new()
    {
        public BrandRepository(ICultureService cultureService, IContentStateService contentStateService, INodeMapper nodeMapper) 
            : base(cultureService, contentStateService, nodeMapper)
        {
        }

        public IList<BrandDto> GetBrandsWithParent()
        {
            return GetBrandsWithParent(CultureInfo.CurrentCulture, ContentState.Draft, ActiveStatus.All);
        }

        public IList<BrandDto> GetBrandsWithParent(CultureInfo culture, ContentState contentState, ActiveStatus status)
        {
            var brands = All(culture, contentState, status);
            IList<BrandDto> brandWithParents = new List<BrandDto>();
            int level = 0;

            foreach (var brand in brands)
            {
                brandWithParents.Add(new BrandDto
                {
                    Name = GetBrandNameWithParent(brand.ParentNodeId.Value, ref level, ref brands) + brand.Name,
                    NodeId = brand.NodeId,
                    TypeId = brand.TypeId
                });
            }

            brandWithParents = brandWithParents.OrderBy(cat => cat.Name).ToList();

            return brandWithParents;
        }


        public virtual string GetBrandNameWithParent(Guid brandId, ref int level, ref IList<TItem> brands)
        {
            const string separator = " \\ ";

            if (brandId.ToString().ToUpper() == "317E6ABA-8454-4A52-9688-D76C17739570")
            {
                level = 0;
                return "";
            }
            var selectedBrands = brands.Where(cat => cat.NodeId == brandId).FirstOrDefault();

            if (selectedBrands == null)
            {
                return "";
            }

            var parentBrandName = GetBrandNameWithParent(selectedBrands.ParentNodeId.Value, ref level, ref brands);

            level += 1;

            if (!parentBrandName.IsNullOrEmpty())
            {
                if (level == 2)
                {
                    return String.Format("{0}{1}{2}", parentBrandName, selectedBrands.Name, separator);
                }
                return String.Format("{0}{1}{2}{3}", parentBrandName, separator, selectedBrands.Name,
                                     separator);
            }
            return String.Format("{0}{1}", selectedBrands.Name, separator);
        }
    }

    public class BrandRepository : BrandRepository<BrandCollection, Brand>, IBrandRepository
    {
        public BrandRepository(ICultureService cultureService, IContentStateService contentStateService, INodeMapper nodeMapper)
            : base(cultureService, contentStateService, nodeMapper)
        {
        }
    }

    public interface IBrandRepository<T> : IMedianamikRepository<T>
        where T : Interfaces.IBrand
    {
        IList<BrandDto> GetBrandsWithParent();
        IList<BrandDto> GetBrandsWithParent(CultureInfo culture, ContentState contentState, ActiveStatus status);
        string GetBrandNameWithParent(Guid categoryId, ref int level, ref IList<T> brands);
    }

    public interface IBrandRepository : IBrandRepository<Brand>
    {
    }
}
